<template>
    <div>
        <el-form :inline="true" :model="role" class="demo-form-inline" label-width="100px">
            <el-form-item label="角色名：">
                <el-input v-model="role.roleName"  placeholder="角色名"   ></el-input>
            </el-form-item>
            <br>
            <el-form-item label="角色描述：">
                <el-input v-model="role.roleText" placeholder="角色描述"></el-input>
            </el-form-item>


            <el-tree style="margin-left: 15px"
                    :data="data"
                    show-checkbox
                    node-key="id"
                    :default-checked-keys="checked"
                    :props="defaultProps"
                    ref="tree">

            </el-tree>
            <el-form-item style="margin-top: 30px;margin-left: 15px">
                <el-button type="primary" @click="add">添加</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import Management_anthorityService from '../../../model/mfy/system_user/Management_anthorityService.js'
    var management_anthorityService  = Management_anthorityService.getInstance();
    export default {
        name: "add_role",
        data(){
            return{
                role:{},
                data: [],
                checked:[],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
            }
        },
        methods:{
            getBigMenu(){
                management_anthorityService.getBigData().then((response)=>{
                    this.data=response.data.data
                });
            },
            add(){

                if(this.role.roleName==null || this.role.roleName == "" ){
                    this.$message.error("角色名不能为空!")
                    return;
                }
                if(this.role.roleText==null || this.role.roleText == "" ){
                    this.$message.error("角色描述不能为空!")
                    return;
                }

                const authorityIds = this.$refs.tree.getHalfCheckedKeys()
                const childrenIds = this.$refs.tree.getCheckedKeys()
                childrenIds.forEach(item => authorityIds.push(item))
                if(authorityIds==null || authorityIds == "" ){
                    this.$message.error("角色权限不能为空!")
                    return;
                }
                var a= {}
                a.roleName=this.role.roleName
                a.roleText=this.role.roleText
                a.id =authorityIds.toString()
                management_anthorityService.addRoleAndanthority(a).then((response)=>{

                    if(response.data.code==400){
                        this.$message.error(response.data.message);
                        this.role.roleName = "";
                        return;
                    }
                    this.$emit("refresh")
                });

            },
            refresh2(){
                this.role={}
            }
        },
        created(){
            this.getBigMenu();
        }
    }
</script>

<style scoped>

</style>
